<?php
class Group extends AppModel {
	var $name = 'Group';
	public $actsAs = array('Acl' => 'requester');
	var $validate = array(
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	
	public $hasMany = array(
		'User',
		'ChildrenGroup' => array(
			'className' => 'Group',
			'foreignKey' => 'group_id'
		)
	);
	
	public $belongsTo = array(
		'ParentGroup' => array(
			'className' => 'Group',
			'foreignKey' => 'group_id'
		)
	);
	
	public function parentNode() {
		if (!$this->id && empty($this->data)) {
			return null;
		}
		$data = $this->data;
		if (empty($this->data)) {
			$data = $this->read();
		}
		if (empty($data['Group']['group_id'])) {
			return null;
		} else {
			return array('Group' => array('id' => $data['Group']['group_id']));
		}
	}
	
	/**	
	 * After save callback
	 *
	 * Update the aro for the user.
	 *
	 * @access public
	 * @return void
	 */
	function afterSave($created) {
			if (!$created) {
				$parent = $this->parentNode();
				$parent = $this->node($parent);
				$node = $this->node();
				$aro = $node[0];
				$aro['Aro']['parent_id'] = $parent[0]['Aro']['id'];
				$this->Aro->save($aro);
			}
	}
}
?>